package com.cxx.java.spring;

import com.cxx.java.spring.ioc.demo.A;
import com.cxx.java.spring.ioc.demo.C;
import com.cxx.java.spring.ioc.demo2.B;
import com.cxx.java.spring.ioc.demo3.DemoA;
import com.cxx.java.spring.ioc.demo3.DemoB;
import com.cxx.java.spring.ioc.demo3.DemoC;
import com.cxx.java.spring.ioc.demo3.DemoD;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
    public static void a() {
        A a = new A();
        C c = new C();
        a.a();
        c.c();
    }

    public static void b() {
        B b = new B();
        com.cxx.java.spring.ioc.demo2.A a = new com.cxx.java.spring.ioc.demo2.A(b);
        a.a();
    }

    public static void c() {
        //加载配置文件
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        //从IoC容器中获取那个对象
        // 获取到名称为da的对象 byName
        Object obj1 = beans.getBean("da");
        //获取到类型为DemoA的对象 byType 注意：使用byType方式配置文件中只能有一个对象
        DemoA obj2 = beans.getBean(DemoA.class);
        //获取到类型为DemoA，名称为da的对象，并且可以解决上一个问题
        // byName+byType
        DemoA obj3 = beans.getBean("da", DemoA.class);
        System.out.println(obj1);
        System.out.println(obj2);
        System.out.println(obj3);
        //其实只new了一次对象
        System.out.println(obj1 == obj2);//true
        System.out.println(obj2 == obj3);//true
    }


    public static void d() {
        ApplicationContext beans = new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoB db = beans.getBean(DemoB.class);
        System.out.println(db);
        DemoC dc = beans.getBean(DemoC.class);
        System.out.println(dc);
    }

    public static void e(){
        ApplicationContext beans=new ClassPathXmlApplicationContext("applicationContext.xml");
        DemoD dd=beans.getBean(DemoD.class);
        System.out.println(dd);

    }

    public static void main(String[] args) {
//        a();
//        b();
//        c();
//        d();
        e();
    }
}
